#include "EmailSenderTask.h"
#include "EmailManager.h"
#include "ConfigValues.h"
#include "Defines.h"
#include "LogManager.h"
#include <process.h>

unsigned __stdcall EmailSenderThreadProc(LPVOID lpParameter)
{
    HANDLE* hEvent = (HANDLE*)lpParameter;
    while (WaitForSingleObject(*hEvent, 0) != WAIT_OBJECT_0)
    {
        unsigned int uiEmailID;
        unsigned int uiMonitorID;
        vector<string> recipients;
        string szEmailBody;
        string szEmailFrom;
        bool bResult = dbLayer->GetEmailInfo(uiEmailID, uiMonitorID, szEmailFrom, recipients, szEmailBody);
        if (bResult && uiEmailID)
        {
            LOGT(("EmailSenderThreadProc. EmailID: %u. MonitorID: %u\n", uiEmailID, uiMonitorID));
            EmailManager emailManager;
            if (emailManager.SendEmail(szEmailBody, szEmailFrom, recipients))
            {
                LOGT(("EmailSenderThreadProc. Email to recipients sended\n"));
                if(dbLayer->SetEmailStatus(uiEmailID, EMAIL_STATUS_SENT))
                    LOGT(("EmailSenderThreadProc. Setting email status completed\n"));

				emailManager.SMSSend(uiMonitorID);
            }
            else
            {
                if (dbLayer->SetEmailStatus(uiEmailID, EMAIL_STATUS_ERROR))
                    LOGT(("EmailSenderThreadProc. Setting email status completed\n"));
            }
        }
        else
        {
            Sleep(SLEEP_WHEN_NOTHING); 
        }        
    }
    LOGT(("EmailSenderThreadProc. Execution completed\n"));
    return 0;
}

EmailSenderTask::EmailSenderTask(void)
{
    hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}

EmailSenderTask::~EmailSenderTask(void)
{
    CloseHandle(hEvent);
}

void EmailSenderTask::InitTask()
{
    for (unsigned long i=0; i<config_emailsender_threads_count; i++)
    {
        unsigned int uiThreadID = 0;
        HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &EmailSenderThreadProc, &hEvent, CREATE_SUSPENDED, &uiThreadID);
        if (!hThread)
        {
            LOGT(("EmailSenderTask::InitTask. Thread creation fail\n"));
        }
        else
        {
            threads.insert(ThreadListPair(uiThreadID, hThread));
            LOGT(("EmailSenderTask::InitTask. Thread created. ThreadID: %u. ThreadHandle 0x%04X\n", uiThreadID, hThread));
        }
    }
}

void EmailSenderTask::StartTask()
{
    for (ThreadListIter i=threads.begin(); i!=threads.end(); i++)
    {
        if (ResumeThread(i->second) == (DWORD)-1)
        {
            LOGT(("EmailSenderTask::StartTask. Thread not started. ThreadID: %u. ThreadHandle 0x%04X\n", i->first, i->second));
        }
        else
        {
            LOGT(("EmailSenderTask::StartTask. Thread started. ThreadID: %u. ThreadHandle 0x%04X\n", i->first, i->second));
        }
    }
}

void EmailSenderTask::StopTask()
{
    SetEvent(hEvent);
    LOGT(("EmailSenderTask::StopTask. Send signal to threads for stopping\n"));
}

void EmailSenderTask::WaitForStop()
{
    for (ThreadListIter i=threads.begin(); i!=threads.end(); i++)
    {
        if (WaitForSingleObject(i->second, INFINITE) == WAIT_OBJECT_0)
        {
            LOGT(("EmailSenderTask::WaitForStop. Handle closed. ThreadID: %u. ThreadHandle 0x%04X\n", i->first, i->second));
            CloseHandle(i->second);
        }
    }
    threads.clear();
}